package com.ruoyi.service.impl;


import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.domain.SceneryareaDto;
import com.ruoyi.mapper.WechartMapper;
import com.ruoyi.service.IWechartService;
import com.ruoyi.system.domain.Sceneryarea;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class WechartServiceImpl implements IWechartService {

    @Autowired
    WechartMapper wechartMapper;


    @Override
    public List<SceneryareaDto> getSceneryareaList(SceneryareaDto sceneryarea) {

        String myLogi=sceneryarea.getMyLogitude();
        String muLati=sceneryarea.getMyLatitude();
        ArrayList<SceneryareaDto> sceneryareaDtos = new ArrayList<>();
        if (myLogi==null&&muLati==null){
            return sceneryareaDtos;
        }
        //获取我的经纬度
        double myLogitude= Double.parseDouble(myLogi);
        double myLatitude=Double.parseDouble(muLati);

        Integer currentPage=sceneryarea.getCurrentPage();
        Integer pageSize = sceneryarea.getPageSize();

        if (currentPage==null||pageSize==null){
                currentPage=1;
                pageSize=10;
            }

        PageHelper.startPage((currentPage-1)*pageSize,pageSize);
        List<Sceneryarea> sceneryareaList = wechartMapper.getSceneryareaList(sceneryarea);
        if (myLogitude!=0&&myLatitude!=0){
            for (Sceneryarea sceneryarea1 : sceneryareaList) {
                double sceneryareaLongitude = Double.parseDouble(sceneryarea1.getSceneryareaLongitude());
                double sceneryareaLatitude = Double.parseDouble(sceneryarea1.getSceneryareaLatitude());
                double distance = getDistance(myLogitude, myLatitude, sceneryareaLongitude, sceneryareaLatitude); //获取离我的距离

                SceneryareaDto sceneryareaDto = new SceneryareaDto();
                //获取景区中最小门票费

                //sceneryareaDto =null;  //清空对象
                BeanUtils.copyProperties(sceneryarea1,sceneryareaDto);
                sceneryareaDto.setDistance(Double.toString(distance));


                sceneryareaDtos.add(sceneryareaDto);

            }
        }
        return sceneryareaDtos;




    }


    //计算距离
    public static double getDistance(double longitudeFrom, double latitudeFrom, double longitudeTo, double latitudeTo) {
        GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom);
        GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo);

        return new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target).getEllipsoidalDistance();
    }

}
